package org.example.struct.array;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author liyishan
 * @date 2023/12/6 10:22
 * @apiNote
 */

public class M0104CanPermutePalindrome {

    public static boolean canPermutePalindrome(String s) {
        Map<Character,Integer> map = new HashMap<>();
        for(char c : s.toCharArray()){
            map.put(c,map.getOrDefault(c,0)+1);
        }
        int count = 0;
        for(Character key :map.keySet()){
            if(map.get(key)%2==1){
                if(++count > 1){
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean test(String s){
        Set<Character> set = new HashSet<>();
        for(char c : s.toCharArray()){
            if(!set.add(c)){
                set.remove(c);
            }
        }
        return set.size() <=1;
    }


    public static boolean test1(String s){
        long front =0,end = 0;
        for(int i = 0; i<s.length(); i++){
            if(s.charAt(i) >= 64){
                end ^= 1L << s.charAt(i) - 64;
            }else{
                front ^= 1L << s.charAt(i);
            }
        }
        return Long.bitCount(front) + Long.bitCount(end) <= 1;
    }

    public static boolean test2(String s){
        int[] map  = new int[128];
        int count = 0;
        for(char c : s.toCharArray()){
            if((map[c]++ & 1) == 1){
                count --;
            }else{
                count ++;
            }
        }
        return count <=1;
    }
}
